package com.tianbao54.kotlin_01

fun main(args: Array<String>) {

    println(convert2Int("24"))
    println(convert2Int("abc"))
    multiply("24","22")
}

fun convert2Int(str:String ): Int? {

    try {
        return str.toInt()
    } catch (ex :NumberFormatException) {
        return null
    }
}

fun multiply (a : String, b : String) {

    var inta = convert2Int(a)
    var intb = convert2Int(b)

    if (null != inta && null != intb) {
        println(inta * intb)
    } else {
        println("param is not integer")
    }
}

fun multiply2 (a:String , b:String) {

    var inta = convert2Int(a)
    var intb = convert2Int(b)

    // 会报错，Int? 不能相乘
//    println(inta * intb)

    // 判断后如果不为空， kotlin认为可以相乘
    if (null == inta) {
        println("param is not integer")
    } else if (null == intb) {
        println("param is not integer")
    } else {
        println(inta * intb)
    }
}